<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="楼座名称" prop="termid">
         <el-select v-model="queryParams.termid" placeholder="楼座名称" clearable>
          <el-option
              v-for="item in terminfoList"
              :key="item.termid"
              :label="item.terminfo"
              :value="item.termid"
              @change="handleQuery"
            />
        </el-select>
      </el-form-item>

     <el-form-item label="销售状态" prop="salestate">
         <el-select v-model="queryParams.salestate" placeholder="销售状态" clearable>
          <el-option
              v-for="item in salestateList"
              :key="item.saleid"
              :label="item.salename"
              :value="item.salename"
              @change="handleQuery"
            />
        </el-select>
      </el-form-item>

      <el-form-item label="业态类型" prop="termType">
         <el-select v-model="queryParams.termType" placeholder="业态类型" clearable>
          <el-option
              v-for="item in termtypeinfoList"
              :key="item.termtypeid"
              :label="item.termtypename"
              :value="item.termtypename"
              @change="handleQuery"
            />
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="单元" prop="unitid">
        <el-input
          v-model="queryParams.unitid"
          placeholder="请输入单元"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="楼层" prop="floor">
        <el-input
          v-model="queryParams.floor"
          placeholder="请输入楼层"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['house_sale:houseinfo:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['house_sale:houseinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="houseinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="房产编号" align="center" prop="hourseid" />
      <el-table-column label="房产名称" align="center" prop="hoursename" />
      <!-- <el-table-column label="单元" align="center" prop="unitid" /> -->
      <el-table-column label="楼层" align="center" prop="floor" />
      <el-table-column label="房号" align="center" prop="hourseno" />
       <el-table-column label="业态类型" align="center" prop="termtype" />
       <el-table-column label="户型名称" align="center" prop="modelname" />
       <el-table-column label="销售面积" align="center" prop="salearea" />
      <el-table-column label="房产单价" align="center" prop="unitprice" />
      <el-table-column label="房产总价" align="center" prop="totalprice" />
      <el-table-column label="销售状态" align="center" prop="salestate" />
      <el-table-column label="是否已结算" align="center" prop="isbalance" />



      <el-table-column label="操作" fixed="right"  width="120px" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['house_sale:houseinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['house_sale:houseinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改房产资料对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px" :inline="true">
        <el-form-item label="楼座名称" prop="terminfo"><!--由小区名称+栋号-->
           <el-select v-model="form.termid" @change="selectTerm" disabled width="100px">
                 <el-option
                  v-for="item in terminfoList"
                  :key="item.termid"
                  :label="item.terminfo"
                  :value="item.termid"
                ></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="房产名称" prop="hoursename"><!--(由小区名称+栋号+单元+楼层+房号组合生成）-->
          <el-input v-model="form.hoursename"  disabled width="100px"/>
        </el-form-item>
         <el-form-item label="房产类型" prop="hoursetype"><!--现房期房-->
            <el-select v-model="form.hoursetype" placeholder="请选择房产类型" width="100px">
                <el-option v-for="h in hoursetypeList" :label="h.name" :value="h.name" :key="h.id"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="业态类型" prop="termtype">
             <el-select v-model="form.termtype"  placeholder="请选择业态类型" width="100px">
                 <el-option
                  v-for="item in termtypeinfoList"
                  :key="item.termtypeid"
                  :label="item.termtypename"
                  :value="item.termtypename"
                  :disabled="false"
                ></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="销售状态" prop="salestate"><!--(关联saleState的saleid字段）-->
           <!-- <el-select v-model="form.salestate" >
                <el-option v-for="s in salestateList" :label="s.name" :value="s.name" :key="s.id"></el-option>
            </el-select> -->
              <el-input v-model="form.salestate" disabled width:100px/>
        </el-form-item>
        <!-- <el-form-item label="单元" prop="unitid">
          <el-input v-model="form.unitid" placeholder="请输入单元" " />
        </el-form-item> -->
        <el-form-item label="楼层" prop="floor">
          <el-input v-model="form.floor" placeholder="请输入楼层"  disabled width:100px/>
        </el-form-item>
        <el-form-item label="房号" prop="hourseno">
          <el-input v-model="form.hourseno" placeholder="请输入房号"  disabled width:100px/>
        </el-form-item>
        <el-form-item label="合同房产号" prop="contractno">
          <el-input v-model="form.contractno" placeholder="请输入合同房产号" width:100px />
        </el-form-item>
         <el-form-item label="装修标准" prop="fitment">
          <el-input v-model="form.fitment" placeholder="请输入装修标准" width:100px />
        </el-form-item>
        <el-form-item label="朝向" prop="direction">
           <el-select v-model="form.direction" placeholder="请选择朝向" width:100px>
                <el-option v-for="di in directionList" :label="di.name" :value="di.name" :key="di.id"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="结构" prop="struts">
           <el-select v-model="form.struts" placeholder="请选择结构" width:100px>
                <el-option v-for="st in strutsList" :label="st.name" :value="st.name" :key="st.id"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="建筑形式" prop="buildstyle">
            <el-select v-model="form.buildstyle" placeholder="请选择建筑形式" width:100px>
                <el-option v-for="b in buildstyleList" :label="b.name" :value="b.name" :key="b.id"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="户型名称" prop="modelname"><!--(关联housemodel表主键）-->
             <el-select v-model="form.modelname"  placeholder="请选择户型" width:100px>
                 <el-option
                  v-for="item in  housemodelList"
                  :key="item.modelid"
                  :label="item.modelname"
                  :value="item.modelname"
                  :disabled="false"
                ></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="几梯几户" prop="lift">
          <el-input v-model="form.lift" placeholder="请输入几梯几户" width:100px />
        </el-form-item>
        <el-form-item label="是否已结算" prop="isbalance">
            <el-input v-model="form.isbalance" disabled/>
            <!-- <el-select v-model="form.isbalance" width:100px>
                <el-option v-for="is in isbalancelist" :label="is.name" :value="is.name" :key="is.id"></el-option>
            </el-select> -->
        </el-form-item>
        <el-form-item label="销售面积(m2)" prop="salearea">
          <el-input v-model="form.salearea" placeholder="请输入销售面积(m2)" @change="gettotalprice" width:100px/>
        </el-form-item>
        <el-form-item label="套内面积(m2)" prop="inarea">
          <el-input v-model="form.inarea" placeholder="请输入套内面积(m2)" width:100px/>
        </el-form-item>
        <el-form-item label="建筑面积(m2)" prop="buildarea">
          <el-input v-model="form.buildarea" placeholder="请输入建筑面积(m2)" width:100px/>
        </el-form-item>
        <el-form-item label="花园面积(m2)" prop="viewarea">
          <el-input v-model="form.viewarea" placeholder="请输入花园面积(m2)" width:100px/>
        </el-form-item>
        <el-form-item label="地下室面积(m2)" prop="downarea">
          <el-input v-model="form.downarea" placeholder="请输入地下室面积(m2)" width:100px/>
        </el-form-item>
        <el-form-item label="公摊面积(m2)" prop="publicarea">
          <el-input v-model="form.publicarea" placeholder="请输入公摊面积(m2)" width:100px/>
        </el-form-item>
        <el-form-item label="阳台面积(m2)" prop="tablearea">
          <el-input v-model="form.tablearea" placeholder="请输入阳台面积(m2)" width:100px/>
        </el-form-item>
        <el-form-item label="露台面积(m2)" prop="outarea">
          <el-input v-model="form.outarea" placeholder="请输入露台面积(m2)" width:100px/>
        </el-form-item>
        <el-form-item label="使用率(%) " prop="usepercent">
          <el-input v-model="form.usepercent" placeholder="请输入使用率(%) " width:100px/>
        </el-form-item>
        <el-form-item label="实测平台面积(m2)" prop="facttablearea">
          <el-input v-model="form.facttablearea" placeholder="请输入实测平台面积(m2)" width:100px/>
        </el-form-item>
        <el-form-item label="实测套内面积(m2)" prop="factinarea">
          <el-input v-model="form.factinarea" placeholder="请输入实测套内面积(m2)" width:100px/>
        </el-form-item>
        <el-form-item label="实测建筑面积(m2)" prop="factbuildarea">
          <el-input v-model="form.factbuildarea" placeholder="请输入实测建筑面积(m2)" width:100px/>
        </el-form-item>
        <el-form-item label="实测花园面积(m2)" prop="factviewarea">
          <el-input v-model="form.factviewarea" placeholder="请输入实测花园面积(m2)" width:100px/>
        </el-form-item>
        <el-form-item label="实测地下室面积(m2)" prop="factdownarea">
          <el-input v-model="form.factdownarea" placeholder="请输入实测地下室面积(m2)" width:100px/>
        </el-form-item>
        <el-form-item label="实测公摊面积(m2)" prop="factpublicarea">
          <el-input v-model="form.factpublicarea" placeholder="请输入实测公摊面积(m2)" width:100px/>
        </el-form-item>
        <el-form-item label="房产单价(元)" prop="unitprice">
          <el-input v-model="form.unitprice" placeholder="请输入房产单价(元)" @change="gettotalprice" width:100px/>
        </el-form-item>
        <el-form-item label="套内建筑面积单价(元)" prop="inunitprice">
          <el-input v-model="form.inunitprice" placeholder="请输入套内建筑面积单价(元)" width:100px/>
        </el-form-item>
        <el-form-item label="建筑面积单价(元)" prop="buildunitprice">
          <el-input v-model="form.buildunitprice" placeholder="请输入建筑面积单价(元)" width:100px/>
        </el-form-item>
        <el-form-item label="房产总价(元)" prop="totalprice">
          <el-input v-model="form.totalprice" placeholder="请输入房产总价(元)"  width:100px/>
        </el-form-item>
        <el-form-item label="原始单价(元)" prop="oldtotalprice">
          <el-input v-model="form.oldtotalprice" placeholder="请输入原始单价(元)" width:100px/>
        </el-form-item>
        <el-form-item label="原始总价(元)" prop="oldunitprice">
          <el-input v-model="form.oldunitprice" placeholder="请输入原始总价(元)" width:100px/>
        </el-form-item>
        <el-form-item label="房产底价(单价)(元)" prop="unitlowprice">
          <el-input v-model="form.unitlowprice" placeholder="请输入房产底价(单价)(元)" width:100px/>
        </el-form-item>
        <el-form-item label="房产底价(总价)(元)" prop="totallowprice">
          <el-input v-model="form.totallowprice" placeholder="请输入房产底价(总价)(元)" width:100px/>
        </el-form-item>
        <el-form-item label="推出时间" prop="saletime">
          <el-date-picker clearable width:100px
            v-model="form.saletime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择推出时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="门牌号" prop="doorno">
          <el-input v-model="form.doorno" placeholder="请输入门牌号" width:100px/>
        </el-form-item>
        <el-form-item label="折扣(%) " prop="discount">
          <el-input v-model="form.discount" placeholder="请输入折扣(%) " width:100px/>
        </el-form-item>
        <el-form-item label="折后价(元)" prop="discountprice">
          <el-input v-model="form.discountprice" placeholder="请输入折后价(元)" width:100px/>
        </el-form-item>
        <el-form-item label="佣金比例" prop="commisionpercent">
          <el-input v-model="form.commisionpercent" placeholder="请输入佣金比例"  width:100px/>
        </el-form-item>
        <el-form-item label="应付佣金" prop="commisionmoney">
          <el-input v-model="form.commisionmoney" placeholder="请输入应付佣金" width:100px/>
        </el-form-item>
        <el-form-item label="已付佣金" prop="commisionpaid">
          <el-input v-model="form.commisionpaid" placeholder="请输入已付佣金" width:100px/>
        </el-form-item>
        <el-form-item label="购房客户编号" prop="custid">
          <el-input v-model="form.custid" placeholder="请输入购房客户编号" width:100px/>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入描述" style="width:600px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";
import { listHouseinfo,getOthers, getHouseinfo, delHouseinfo, addHouseinfo, updateHouseinfo } from "@/api/house_sale/houseinfo";

export default {
  name: "Houseinfo",
  dicts: ['sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      isbalancelist:[{id:1,name:'是'},
                  {id:2,name:'否'}
      ],
      // 总条数
      total: 0,
      // 房产资料表格数据
      houseinfoList: [],
      terminfoList: [],
      termtypeinfoList: [],
      housemodelList: [],
      salestateList:[],

      hoursetypeList:[ {id:0,name:'期房'},  {id:1,name:'现房'} ],
      salestateList:[ {id:0,name:'可售'}],
      directionList:[
        {id:0,name:'东'},  {id:1,name:'南'} , {id:2,name:'西'},  {id:3,name:'北'},
        {id:4,name:'东北'},  {id:5,name:'东南'} , {id:6,name:'西北'},  {id:7,name:'西南'}
      ],
      strutsList:[ {id:0,name:'小高层'},  {id:1,name:'半高层'} , {id:2,name:'现浇煎力墙'},  {id:3,name:'框架煎力墙'} ],
      buildstyleList:[ {id:0,name:'现代'},  {id:1,name:'复古'} ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 导入数据参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: true,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/house_sale/houseinfo/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        termid: null,
        hoursename: null,
        unitid: null,
        floor: null,
        salestate: null,
        termType:null
      },
      // 表单参数
      form: {},

      // 表单校验
      rules: {
        termid: [
          { required: true, message: "楼栋编号不能为空", trigger: "blur" }
        ],
        hoursename: [
          { required: true, message: "房产名称不能为空", trigger: "blur" }
        ],
        hoursetype: [
          { required: true, message: "房产类型不能为空", trigger: "change" }
        ],
        termtype: [
          { required: true, message: "业态类型不能为空", trigger: "change" }
        ],
        unitid: [
          { required: true, message: "单元不能为空", trigger: "blur" }
        ],
        floor: [
          { required: true, message: "楼层不能为空", trigger: "blur" }
        ],
        hourseno: [
          { required: true, message: "房号不能为空", trigger: "blur" }
        ],
        salestate: [
          { required: true, message: "销售状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();

  },
  methods: {
    /**计算佣金 */
    // getcommisionmoney(){
    //  if(this.form.totalprice!=null && this.form.commisionpercent!=null){
    //     this.form.commisionmoney=this.form.totalprice*this.form.commisionpercent
    //   }
    // },
    /*自动获取总价*/
    gettotalprice(){
      if(this.form.salearea!=null && this.form.unitprice!=null){
        this.form.totalprice=this.form.salearea*this.form.unitprice
      }
    },
    /**自动获取楼座信息 */
    selectTerm(temp){
      console.log("temp:"+temp);
      this.terminfoList.forEach(element => {
        if(element.termid === temp){
            this.form.hoursename=element.terminfo;
        }
      });

    },


    /** 查询房产资料列表 */
    getList() {
      this.loading = true;
      listHouseinfo(this.queryParams).then(response => {
        this.houseinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
       getOthers().then(response => {
        this.termtypeinfoList = response.termtypeinfoList;
        this.terminfoList = response.terminfoList;
        this.salestateList = response.salestateList;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        hourseid: null,
        termid: null,
        hoursename: null,
        hoursetype: "现房",
        termtype: this.termtypeinfoList[0].termtypename,
        fitment: null,
        unitid: null,
        floor: null,
        hourseno: null,
        contractno: null,
        salestate: "可售",
        direction: "东",
        struts: "小高层",
        buildstyle: "现代",

        modelname: null,
        lift: null,
        isbalance: "否",
        salearea: null,
        inarea: null,
        buildarea: null,
        viewarea: null,
        downarea: null,
        publicarea: null,
        tablearea: null,
        outarea: null,
        usepercent: null,
        facttablearea: null,
        factinarea: null,
        factbuildarea: null,
        factviewarea: null,
        factdownarea: null,
        factpublicarea: null,
        unitprice: null,
        inunitprice: null,
        buildunitprice: null,
        totalprice: null,
        oldtotalprice: null,
        oldunitprice: null,
        unitlowprice: null,
        totallowprice: null,
        saletime: null,
        doorno: null,
        discount: null,
        discountprice: null,
        commisionpercent: null,
        commisionmoney: null,
        commisionpaid: null,
        custid: null,
        description: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.hourseid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
        getOthers().then(response => {
        this.termtypeinfoList = response.termtypeinfoList;
        this.housemodelList = response.housemodelList;
        this.terminfoList = response.terminfoList;

        this.open = true;
        this.title = "添加房产资料";
      });

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const hourseid = row.hourseid || this.ids
      getHouseinfo(hourseid).then(response => {
        this.termtypeinfoList = response.termtypeinfoList;
        this.housemodelList = response.housemodelList;
        this.terminfoList = response.terminfoList;

        this.form = response.hourseinfo;
        this.open = true;
        this.title = "修改房产资料";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.hourseid != null) {
            updateHouseinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHouseinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const hourseids = row.hourseid || this.ids;
      this.$modal.confirm('是否确认删除房产资料编号为"' + hourseids + '"的数据项？').then(function() {
        return delHouseinfo(hourseids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('house_sale/houseinfo/export', {
        ...this.queryParams
      }, `houseinfo_${new Date().getTime()}.xlsx`)
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('house_sale/houseinfo/importTemplate', {
      }, `houseinfo_template_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "方案流程导入";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
